home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Tools / Languages / Python 1.1 / Modules / syslogmodule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  5.8 KB  |  201 lines  |  [TEXT/KAHL]

  1. /***********************************************************
  2. Copyright 1994 by Lance Ellinghouse,
  3. Cathedral City, California Republic, United States of America.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the name of Lance Ellinghouse
  12. not be used in advertising or publicity pertaining to distribution 
  13. of the software without specific, written prior permission.
  14.  
  15. LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE BE LIABLE FOR ANY SPECIAL, 
  18. INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 
  19. FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 
  20. NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 
  21. WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* syslog module */
  26.  
  27. #include "allobjects.h"
  28. #include "modsupport.h"
  29.  
  30. #include <syslog.h>
  31.  
  32. #include "rename1.h"
  33.  
  34. static PyObject * 
  35. syslog_openlog(self, args)
  36.      PyObject * self;
  37.      PyObject * args;
  38. {
  39.   char *ident = "";
  40.   PyObject * ident_o;
  41.   long logopt = LOG_PID;
  42.   long facility = LOG_USER;
  43.   if (!PyArg_Parse(args, "(Sll);ident string, logoption, facility", &ident_o, &logopt, &facility))
  44.     if (!PyArg_Parse(args, "(Sl);ident string, logoption", &ident_o, &logopt))
  45.       if (!PyArg_Parse(args, "S;ident string", &ident_o))
  46.     return NULL;
  47.   Py_INCREF(ident_o); /* This is needed because openlog() does NOT make a copy
  48.               and syslog() later uses it.. cannot trash it. */
  49.   ident = PyString_AsString(ident_o);
  50.   openlog(ident,logopt,facility);
  51.   Py_INCREF(Py_None);
  52.   return Py_None;
  53. }
  54.  
  55. static PyObject * 
  56. syslog_syslog(self, args)
  57.      PyObject * self;
  58.      PyObject * args;
  59. {
  60.   int   priority = LOG_INFO;
  61.   char *message;
  62.  
  63.   if (!PyArg_Parse(args,"(is);priority, message string",&priority,&message))
  64.     if (!PyArg_Parse(args,"s;message string",&message))
  65.       return NULL;
  66.   syslog(priority, message);
  67.   Py_INCREF(Py_None);
  68.   return Py_None;
  69. }
  70.  
  71. static PyObject * 
  72. syslog_closelog(self, args)
  73.      PyObject * self;
  74.      PyObject * args;
  75. {
  76.     if (!PyArg_NoArgs(args))
  77.         return NULL;
  78.     closelog();
  79.     Py_INCREF(Py_None);
  80.     return Py_None;
  81. }
  82.  
  83. static PyObject * 
  84. syslog_setlogmask(self, args)
  85.      PyObject * self;
  86.      PyObject * args;
  87. {
  88.   long maskpri;
  89.   if (!PyArg_Parse(args,"l;mask for priority",&maskpri))
  90.     return NULL;
  91.   setlogmask(maskpri);
  92.   Py_INCREF(Py_None);
  93.   return Py_None;
  94. }
  95.  
  96. static PyObject * 
  97. syslog_log_mask(self, args)
  98.      PyObject * self;
  99.      PyObject * args;
  100. {
  101.   long mask;
  102.   long pri;
  103.   if (!PyArg_Parse(args,"l",&pri))
  104.     return NULL;
  105.   mask = LOG_MASK(pri);
  106.   return PyInt_FromLong(mask);
  107. }
  108.  
  109. static PyObject * 
  110. syslog_log_upto(self, args)
  111.      PyObject * self;
  112.      PyObject * args;
  113. {
  114.   long mask;
  115.   long pri;
  116.   if (!PyArg_Parse(args,"l",&pri))
  117.     return NULL;
  118.   mask = LOG_UPTO(pri);
  119.   return PyInt_FromLong(mask);
  120. }
  121.  
  122. /* List of functions defined in the module */
  123.  
  124. static PyMethodDef syslog_methods[] = {
  125.     {"openlog",        (PyCFunction)syslog_openlog},
  126.     {"closelog",        (PyCFunction)syslog_closelog},
  127.     {"syslog",              (PyCFunction)syslog_syslog},
  128.     {"setlogmask",          (PyCFunction)syslog_setlogmask},
  129.     {"LOG_MASK",            (PyCFunction)syslog_log_mask},
  130.     {"LOG_UPTO",            (PyCFunction)syslog_log_upto},
  131.     {NULL,        NULL}        /* sentinel */
  132. };
  133.  
  134. /* Initialization function for the module */
  135.  
  136. void
  137. initsyslog()
  138. {
  139.     PyObject *m, *d, *x;
  140.  
  141.     /* Create the module and add the functions */
  142.     m = Py_InitModule("syslog", syslog_methods);
  143.  
  144.     /* Add some symbolic constants to the module */
  145.     d = PyModule_GetDict(m);
  146.     x = PyInt_FromLong(LOG_EMERG);
  147.     PyDict_SetItemString(d, "LOG_EMERG", x);
  148.     x = PyInt_FromLong(LOG_ALERT);
  149.     PyDict_SetItemString(d, "LOG_ALERT", x);
  150.     x = PyInt_FromLong(LOG_CRIT);
  151.     PyDict_SetItemString(d, "LOG_CRIT", x);
  152.     x = PyInt_FromLong(LOG_ERR);
  153.     PyDict_SetItemString(d, "LOG_ERR", x);
  154.     x = PyInt_FromLong(LOG_WARNING);
  155.     PyDict_SetItemString(d, "LOG_WARNING", x);
  156.     x = PyInt_FromLong(LOG_NOTICE);
  157.     PyDict_SetItemString(d, "LOG_NOTICE", x);
  158.     x = PyInt_FromLong(LOG_INFO);
  159.     PyDict_SetItemString(d, "LOG_INFO", x);
  160.     x = PyInt_FromLong(LOG_DEBUG);
  161.     PyDict_SetItemString(d, "LOG_DEBUG", x);
  162.     x = PyInt_FromLong(LOG_PID);
  163.     PyDict_SetItemString(d, "LOG_PID", x);
  164.     x = PyInt_FromLong(LOG_CONS);
  165.     PyDict_SetItemString(d, "LOG_CONS", x);
  166.     x = PyInt_FromLong(LOG_NDELAY);
  167.     PyDict_SetItemString(d, "LOG_NDELAY", x);
  168.     x = PyInt_FromLong(LOG_NOWAIT);
  169.     PyDict_SetItemString(d, "LOG_NOWAIT", x);
  170.     x = PyInt_FromLong(LOG_KERN);
  171.     PyDict_SetItemString(d, "LOG_KERN", x);
  172.     x = PyInt_FromLong(LOG_USER);
  173.     PyDict_SetItemString(d, "LOG_USER", x);
  174.     x = PyInt_FromLong(LOG_MAIL);
  175.     PyDict_SetItemString(d, "LOG_MAIL", x);
  176.     x = PyInt_FromLong(LOG_DAEMON);
  177.     PyDict_SetItemString(d, "LOG_DAEMON", x);
  178.     x = PyInt_FromLong(LOG_LPR);
  179.     PyDict_SetItemString(d, "LOG_LPR", x);
  180.     x = PyInt_FromLong(LOG_LOCAL0);
  181.     PyDict_SetItemString(d, "LOG_LOCAL0", x);
  182.     x = PyInt_FromLong(LOG_LOCAL1);
  183.     PyDict_SetItemString(d, "LOG_LOCAL1", x);
  184.     x = PyInt_FromLong(LOG_LOCAL2);
  185.     PyDict_SetItemString(d, "LOG_LOCAL2", x);
  186.     x = PyInt_FromLong(LOG_LOCAL3);
  187.     PyDict_SetItemString(d, "LOG_LOCAL3", x);
  188.     x = PyInt_FromLong(LOG_LOCAL4);
  189.     PyDict_SetItemString(d, "LOG_LOCAL4", x);
  190.     x = PyInt_FromLong(LOG_LOCAL5);
  191.     PyDict_SetItemString(d, "LOG_LOCAL5", x);
  192.     x = PyInt_FromLong(LOG_LOCAL6);
  193.     PyDict_SetItemString(d, "LOG_LOCAL6", x);
  194.     x = PyInt_FromLong(LOG_LOCAL7);
  195.     PyDict_SetItemString(d, "LOG_LOCAL7", x);
  196.  
  197.     /* Check for errors */
  198.     if (PyErr_Occurred())
  199.         Py_FatalError("can't initialize module syslog");
  200. }
  201.